import tornado.web
import tornado.ioloop
import tornado.httpserver
import json
import tornado.gen  # 并发
from concurrent.futures import  ThreadPoolExecutor  # 采用线程池来管理

class HelloHandler(tornado.web.RequestHandler):

    # 定义线程池数量一定要命名为executor
    executor = ThreadPoolExecutor(1000)
    @tornado.gen.coroutine
    def get(self):
        #协程
        yield self.get_response()

    @tornado.concurrent.run_on_executor
    def get_response(self):
        foodlist = [
            {
                "name": "小鸡炖磨菇",
                        "price": 50.00,
                        "image":  "https://img0.baidu.com/it/u=2348637602,2465629853&fm=26&fmt=auto&gp=0.jpg"
            },
            {
                "name": "地三鲜",
                        "price": 15.00,
                        "image": "https://img1.baidu.com/it/u=32713918,3342462783&fm=26&fmt=auto&gp=0.jpg"
            },
            {
                "name": "小鸡炖磨菇",
                        "price": 50.00,
                        "image": "https://img0.baidu.com/it/u=2348637602,2465629853&fm=26&fmt=auto&gp=0.jpg"
            },
            {
                "name": "小鸡炖磨菇",
                        "price": 50.00,
                        "image": "https://img0.baidu.com/it/u=2348637602,2465629853&fm=26&fmt=auto&gp=0.jpg"
            },
            {
                "name": "地三鲜",
                        "price": 15.00,
                        "image": "https://img1.baidu.com/it/u=32713918,3342462783&fm=26&fmt=auto&gp=0.jpg"
            },
            {
                "name": "小鸡炖磨菇",
                        "price": 50.00,
                        "image": "https://img0.baidu.com/it/u=2348637602,2465629853&fm=26&fmt=auto&gp=0.jpg"
            },


        ]

        # 列表需要转化为json数据
        foods = json.dumps(foodlist)

        # 设置头部
        self.set_header("Content-Type", "application/json;charset=UTF-8")
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "Content-Type")

        self.write(foods)


if __name__ == "__main__":
    application = tornado.web.Application(
        handlers=[
            (r"/", HelloHandler)
        ]
    )

    http_server = tornado.httpserver.HTTPServer(application)
    # 监听端口
    http_server.listen(8001)
    tornado.ioloop.IOLoop.instance().start()






